<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal"  data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="delTemplates()" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  >									
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="template">
			                          <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>手机</td>
									      <td>联想,华为,中兴</td>
									      <td>网络制式</td>									 
									      <td></td>		                                                                  
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 新增窗口 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板新增</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input id="typeName" class="form-control" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td id="brandBody">
		      			
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td id="specBody">
	
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" onclick="addItemHtml()" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="itemBody">
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" onclick="delItemHtml(this)" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" onclick="delItemHtml(this)" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="addTemplate()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  </div>
  <script>
	  $(function () {
		  listTemplate()
		  listBrand()
		  listSpec()
	  })
	  function addTemplate() {
		  var name = $("#typeName").val();

		  var brandList = $("#brandSelect").select2("data")
		  var arr =[]
		  $(brandList).each(function (i,e) {
			var brands ={id:e.id,text:e.text}
			arr.push(brands)
		  })
		  var brandIds = JSON.stringify(arr)

		  var specList = $("#specSelect").select2("data")
		  var arr1 =[]
		  $(specList).each(function (i,e) {
			  var specs ={id:e.id,text:e.text}
			  arr1.push(specs)
		  })
		  var specIds = JSON.stringify(arr1)

		  var trs = $("#itemBody").find("tr")
		  var arr2 = []
		  $(trs).each(function (i,e) {
			  var tds = $(e).find("td")
			  var text = $(tds[1]).find("input").val();
			  var items = {text:text}
			  arr2.push(items)
		  })
		  var custom = JSON.stringify(arr2)

		  $.ajax({
			  url:"/typeTemplate/addTemplate",
			  data:{name:name,specIds:specIds,brandIds:brandIds,customAttributeItems:custom},
			  type:"post",
			  dataType:"json",
			  async:false,
			  success:function (result) {
				  alert(result.message)
				  window.location.reload();
			  },
			  error:function (result) {
				  alert("系统错误")
			  }
		  })
	  }
	  function listBrand() {
		  $.ajax({
			  url:"/brand/listBrand",
			  data:{pageSize:999},
			  type:"post",
			  dataType:"json",
			  async:false,
			  success:function (result) {
			  	var brands = result.data.rows
					if (result.code == 10000){
						var html = '<select id="brandSelect" style="width:100%" multiple>'
						$(brands).each(function (i,e) {
							html+='<option value="'+e.id+'">'+e.name+'</option>'
						})
						html+='</select>'
						$("#brandBody").html(html)
						$("#brandSelect").select2()
					}else {
						alert(result.message)
					}
			  },
			  error:function (result) {
				  alert("系统错误")
			  }
		  })
	  }
	  function listBrand1() {
		  $.ajax({
			  url:"/brand/listBrand",
			  data:{pageSize:999},
			  type:"post",
			  dataType:"json",
			  async:false,
			  success:function (result) {
				  var brands = result.data.rows
				  if (result.code == 10000){
					  var html = '<select id="brandSelect1" style="width:100%" multiple>'
					  $(brands).each(function (i,e) {
						  html+='<option value="'+e.id+'">'+e.name+'</option>'
					  })
					  html+='</select>'
					  $("#brandBody1").html(html)
					  $("#brandSelect1").select2()
				  }else {
					  alert(result.message)
				  }
			  },
			  error:function (result) {
				  alert("系统错误")
			  }
		  })
	  }
	  function listSpec() {
		  $.ajax({
			  url:"/spec/listSpec",
			  data:{},
			  type:"post",
			  dataType:"json",
			  async:false,
			  success:function (result) {
				  var specs = result.data;
				  if (result.code == 10000){
					  var html = '<select id="specSelect" style="width:100%" multiple>'
					  $(specs).each(function (i,e) {
						  html+='<option value="'+e.id+'">'+e.specName+'</option>'
					  })
					  html+='</select>'
					  $("#specBody").html(html)
					  $("#specSelect").select2()
				  }else {
					  alert(result.message)
				  }
			  },
			  error:function (result) {
				  alert("系统错误")
			  }
		  })
	  }
	  function listSpec1() {
		  $.ajax({
			  url:"/spec/listSpec",
			  data:{},
			  type:"post",
			  dataType:"json",
			  async:false,
			  success:function (result) {
				  var specs = result.data;
				  if (result.code == 10000){
					  var html = '<select id="specSelect1" style="width:100%" multiple>'
					  $(specs).each(function (i,e) {
						  html+='<option value="'+e.id+'">'+e.specName+'</option>'
					  })
					  html+='</select>'
					  $("#specBody1").html(html)
					  $("#specSelect1").select2()
				  }else {
					  alert(result.message)
				  }
			  },
			  error:function (result) {
				  alert("系统错误")
			  }
		  })
	  }
	  function listTemplate() {
		  $.ajax({
			  url:"/typeTemplate/listTemplate",
			  data:{},
			  dataType:"json",
			  type:"post",
			  async:false,
			  success:function (result) {
				  var templates = result.data
				  if (result.code == 10000){
					  var html = ''
				  	$(templates).each(function (i,e) {
						var specIds = JSON.parse(e.specIds)
						var brandIds = JSON.parse(e.brandIds)
						var items = JSON.parse(e.customAttributeItems)

						html +='<tr>'
						html +='<td><input name="box" value="'+e.id+'" type="checkbox"></td>'
						html +='<td>'+e.id+'</td>'
						html +='<td>'+e.name+'</td>'

						var specstr = ''
						$(specIds).each(function (i,e) {
							specstr += e.text+","
						})
						specstr = specstr.substring(0,specstr.length-1)
						var brandstr = ''
						$(brandIds).each(function (i,e) {
							brandstr += e.text+","
						})
						brandstr = brandstr.substring(0,brandstr.length-1)
						var itemstr = ''
						$(items).each(function (i,e) {
							itemstr += e.text+","
						})
						itemstr = itemstr.substring(0,itemstr.length-1)

						html +='<td>'+specstr+'</td>'
						html +='<td>'+brandstr+'</td>'
						html +='<td>'+itemstr+'</td>'
						html +='<td class="text-center">'
						html +='<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate('+e.id+')" data-target="#editModal" >修改</button>'
						html +='</td>'
						html +='</tr>'
					})
					  $("#template").html(html);
				  }else {
				  	alert(result.message)
				  }
			  },
			  error:function (result) {
					alert("系统错误")
			  }
		  })
	  }
	  function toUpdate(id) {
		  $.ajax({
			  url:"/typeTemplate/toUpdate",
			  data:{id:id},
			  type:"post",
			  dataType:"json",
			  async:false,
			  success:function (result) {
			  	var type = result.data;
				  if (result.code == 10000){
				  	var html = ''
					  html+='<div class="modal-dialog">'
					  html+='<div class="modal-content">'
					  html+='<div class="modal-header">'
					  html+='<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
					  html+='<h3 id="myModalLabel">商品类型模板新增</h3>'
					  html+='</div>'
					  html+='<div class="modal-body">'
					  html+='<table class="table table-bordered table-striped"  width="800px">'
					  html+='<input type="hidden" id="typeId" value="'+type.id+'">'
					  html+='<tr>'
					  html+='<td>商品类型</td>'
					  html+='<td><input id="typeName1" value="'+type.name+'" class="form-control" placeholder="商品类型">  </td>'
					  html+='</tr>'
					  html+='<tr>'
					  html+='<td>关联品牌</td>'
					  html+='<td id="brandBody1">'
					  html+='</td>'
					  html+='</tr>'
					  html+='<tr>'
					  html+='<td>关联规格</td>'
					  html+='<td id="specBody1">'
					  html+='</td>'
					  html+='</tr>'
					  html+='<tr>'
					  html+='<td>扩展属性</td>'
					  html+='<td>'
					  html+='<div class="btn-group">'
					  html+='<button type="button" class="btn btn-default" onclick="addItemHtml1()" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>'
					  html+='</div>'
					  html+='<table class="table table-bordered table-striped"  width="800px">'
					  html+='<thead>'
					  html+='<tr>'
					  html+='<td><input type="checkbox" class="icheckbox_square-blue"></td>'
					  html+='<td>属性名称</td>'
					  html+='<td>操作</td>'
					  html+='</tr>'
					  html+='</thead>'
					  html+='<tbody id="itemBody1">'

					  var itemIds = JSON.parse(type.customAttributeItems)
					  $(itemIds).each(function (i,e) {
						  html+='<tr>'
						  html+='<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
						  html+='<td><input class="form-control" value="'+e.text+'" placeholder="属性名称" ></td>'
						  html+='<td><button type="button" class="btn btn-default" onclick="delItemHtml(this)" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>'
						  html+='</tr>'
					  })

					  html+='</tbody>'
					  html+='</table>'
					  html+='</td>'
					  html+='</tr>'
					  html+='</table>'
					  html+='</div>'
					  html+='<div class="modal-footer">'
					  html+='<button class="btn btn-success" data-dismiss="modal" onclick="updateTemplate('+type.id+')" aria-hidden="true">保存</button>'
					  html+='<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>'
					  html+='</div>'
					  html+='</div>'
					  html+='</div>'

					  $("#editModal").html(html);
					  listBrand1();
					  listSpec1();

					  var specIds = JSON.parse(type.specIds)
					  var arr =[]
					  $(specIds).each(function (i,e) {
							arr.push(e.id)
					  })
					  $("#specSelect1").val(arr).trigger("change")

					  var brandIds = JSON.parse(type.brandIds)
					  var arr1 =[]
					  $(brandIds).each(function (i,e) {
						  arr1.push(e.id)
					  })
					  $("#brandSelect1").val(arr1).trigger("change")
				  }
			  },
			  error:function (result) {
				  alert("系统错误")
			  }
		  })
	  }
	  function updateTemplate(id) {
		  var name = $("#typeName1").val();

		  var brandList = $("#brandSelect1").select2("data")
		  var arr =[]
		  $(brandList).each(function (i,e) {
			  var brands ={id:e.id,text:e.text}
			  arr.push(brands)
		  })
		  var brandIds = JSON.stringify(arr)

		  var specList = $("#specSelect1").select2("data")
		  var arr1 =[]
		  $(specList).each(function (i,e) {
			  var specs ={id:e.id,text:e.text}
			  arr1.push(specs)
		  })
		  var specIds = JSON.stringify(arr1)

		  var trs = $("#itemBody1").find("tr")
		  var arr2 = []
		  $(trs).each(function (i,e) {
			  var tds = $(e).find("td")
			  var text = $(tds[1]).find("input").val();
			  var items = {text:text}
			  arr2.push(items)
		  })
		  var custom = JSON.stringify(arr2)

		  $.ajax({
			  url:"/typeTemplate/updateTemplate",
			  data:{id:id,name:name,specIds:specIds,brandIds:brandIds,customAttributeItems:custom},
			  type:"post",
			  dataType:"json",
			  async:false,
			  success:function (result) {
				  alert(result.message)
				  window.location.reload();
			  },
			  error:function (result) {
				  alert("系统错误")
			  }
		  })
	  }
	  function delTemplates() {
		  if(confirm("确定删除吗？")){
			  var boxs = document.getElementsByName("box")
			  var ids = ''
			  $(boxs).each(function (i,e) {
				  if (e.checked){
					  ids+= $(e).val()+","
				  }
			  })
			  ids = ids.substring(0,ids.length-1)
			  $.ajax({
				  url:"/typeTemplate/delTemplates",
				  data:{ids:ids},
				  type:"post",
				  dataType:"json",
				  async:false,
				  success:function (result) {
					  alert(result.message)
					  window.location.reload();
				  },
				  error:function (result) {
					  alert("系统错误")
				  }
			  })
		  }
	  }
	  function addItemHtml() {
		  var html = ''
		  html +='<tr>'
		  html +='<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
		  html +='<td><input class="form-control" placeholder="属性名称" ></td>'
		  html +='<td><button type="button" class="btn btn-default" onclick="delItemHtml(this)" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>'
		  html +='</tr>'
		  $("#itemBody").append(html)
	  }
	  function addItemHtml1() {
		  var html = ''
		  html +='<tr>'
		  html +='<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
		  html +='<td><input class="form-control" placeholder="属性名称" ></td>'
		  html +='<td><button type="button" class="btn btn-default" onclick="delItemHtml(this)" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>'
		  html +='</tr>'
		  $("#itemBody1").append(html)
	  }
	  function delItemHtml(but) {
	  	if (confirm("确定删除吗?")){
			var tr = but.parentNode.parentNode
			tr.parentNode.removeChild(tr)
		}
	  }
  </script>
</body>
</html>